# !/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
@作者 杨阳
@时间 2018-06-21
@说明 处理角色相关的请求
@注意事项
"""

from flask import Blueprint, request
from flask_login import current_user

from model.SystemModel import TRoleModel

role = Blueprint('role', __name__, url_prefix='/role')


@role.route("/delete/<id>", methods=["DELETE"])
def delete_role(id):
    """
    删除角色
    :param id:  角色id
    :return:
    """
    return TRoleModel.delete_role(id=id)


@role.route("/save/", methods=["POST"])
def save_role():
    """
    增加或更新角色
    :return:
    """
    id = request.form.get("id")
    name = request.form.get("name")
    comment = request.form.get("comment")
    resource_ids = request.form.get("resource_ids")

    if id and "0" != id:
        return TRoleModel.update_role(id=id, name=name, comment=comment, resource_ids=resource_ids)
    else:
        return TRoleModel.add_role(name=name, comment=comment, resource_ids=resource_ids)


@role.route("/grid/", methods=["POST"])
def role_grid():
    """
    查询角色表格
    :return: json 字符串
    """
    current_page = request.form.get("current_page")
    search_name = request.form.get("search_name")
    search_start_data = request.form.get("search_start_data")
    search_end_data = request.form.get("search_end_data")

    return TRoleModel.get_role_grid(current_page=current_page,
                                    search_name=search_name,
                                    search_start_data=search_start_data,
                                    search_end_data=search_end_data)


@role.route("/get/<id>", methods=["GET"])
def get_role_byId(id):
    """
    根据角色id查询角色
    :param id:  角色id
    :return:
    """
    return TRoleModel.get_role_byId(id)


@role.route("/tree/<id>", methods=["GET"])
def get_role_resource_tree(id):
    """
    获得角色包含的权限，并且默认选中当前用户拥有的权限
    :param id:  角色id
    :return:
    """
    return TRoleModel.get_role_resource_tree(id, current_user.id)
